import redis from 'ioredis'
import elementUI from 'element-ui'
import bluebird from 'bluebird'
bluebird.promisifyAll(redis)

export default{
  createConnection(item){
    //集群模式
    if(item.Model==='Cluster'){
      return this.createClusterConnection(item)
    }
    //哨兵模式
    if(item.Model==='Sentinels'){
      return this.createSentinelConnection(item)
    }
    //单机模式
    return this.createStandAloneConnection(item)
  },
  //单机连接模式
  createStandAloneConnection (item) {
    const options = {
      host:item.Host.split(":")[0],
      port:parseInt(item.Host.split(":")[1]), 
      connectTimeout: 1000,
      retryStrategy: (options) => { return this.retryStragety(options)},
      password: item.Auth,
      showFriendlyErrorStack:true
    }
    const _redis=new redis(options);
    _redis.on('error',(err)=>{
      this.exceptionHandler(err)
    })
    return _redis;
  },
  //哨兵集群模式连接
  createSentinelConnection(item){
    var sentinels=this.generateNode(item.Host);
    const options = {
      sentinels:sentinels,
      connectTimeout: 1000,
      sentinelRetryStrategy: (options) => { return this.retryStragety(options)},
      sentinelPassword: item.Auth,
      showFriendlyErrorStack:true
    }
    const _sentinel=new redis(options)
    //错误处理
    _sentinel.on("error",(err)=>{
      this.exceptionHandler(err)
    })
    return _sentinel
  },
  //Cluster集群模式连接
  createClusterConnection(item){
    var clusters=this.generateNode(item.Host);
    const options = {
      connectTimeout: 1000,
      password: item.Auth
    }
    const cluster=new redis.Cluster(clusters,
      {
        clusterRetryStrategy:(options) => { return this.retryStragety(options)},
        redisOptions:options,
        scaleReads:'all',
        showFriendlyErrorStack:true
      }
    )
    //错误处理
    cluster.on("error",(err)=>{
      this.exceptionHandler(err)
    })
    return cluster;
  },
  //重试机制
  retryStragety (options) {
    return false;
  },
  generateNode(host){
    var node =[];
    var arrays=host.split(",")
    for (let i = 0; i < arrays.length; i++) {
      node.push({"host":arrays[i].split(":")[0],"port":parseInt(arrays[i].split(":")[1])})
    }
    return node;
  },
  exceptionHandler(err){
    //密码错误
    if(err.message==="ERR invalid password"){
      elementUI.Message({
        type: 'error',
        message: window.$Vue.$t('RedisOperating.passWordError')
      })
    //连接超时
    }else if(err.message==="connect ETIMEDOUT"){
      elementUI.Message({
        type: 'error',
        message: window.$Vue.$t('RedisOperating.connectionError')
      })
    //其他异常处理
    }else if(err.message==="NOAUTH Authentication required."){
      elementUI.Message({
        type: 'error',
        message: window.$Vue.$t('RedisOperating.NeedPwd')
      })
    //集群连接失败无法刷新
    }else if(err.message==="Failed to refresh slots cache."){
      elementUI.Message({
        type: 'error',
        message: window.$Vue.$t('RedisOperating.FailedRefreshSlots')
      })
    }else{
      elementUI.Message({
        type: 'error',
        message: err.message
      })
    }
    return false;
  }
}
